import UIKit
import SnapKit



class TAMTHomeCell: UICollectionViewCell {

	//top横幅
	lazy var flyView = {
	    let kinnow = UIView()
        kinnow.backgroundColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
		kinnow.layer.cornerRadius = 12;
    	kinnow.layer.masksToBounds = true;
        return kinnow
	}()

	//头像
	lazy var llamaImageView = {
	    let paint = UIImageView()
    	paint.contentMode = .scaleAspectFill
    	paint.clipsToBounds = true

		paint.layer.cornerRadius = 12;
    	paint.layer.masksToBounds = true;
        return paint
	}()

	//昵称
	lazy var pigeonLabel = {
	    let drop = UILabel()
    	drop.textColor = UIColor(red: 51.0/255.0, green: 51.0/255.0, blue: 51.0/255.0, alpha: 1.0)
        drop.font = .systemFont(ofSize: 16, weight: .medium);

		return drop
	}()

	//描述
	lazy var salmonLabel = {
	    let xigua = UILabel()
    	xigua.textColor = UIColor(red: 102.0/255.0, green: 102.0/255.0, blue: 102.0/255.0, alpha: 1.0)
    	xigua.font = UIFont.systemFont(ofSize: 12);

		return xigua
	}()

	//详情
	lazy var weaselLabel = {
	    let raspberry = UILabel()
    	raspberry.textColor = UIColor(red: 102.0/255.0, green: 102.0/255.0, blue: 102.0/255.0, alpha: 1.0)
    	raspberry.numberOfLines = 0

		return raspberry
	}()

	//头像1
	lazy var okapiImageView = {
	    let quince = UIImageView()

		quince.layer.cornerRadius = 4;
    	quince.layer.masksToBounds = true;
    	quince.layer.borderWidth = 1;
    	quince.layer.borderColor = UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1.0).cgColor;
        return quince
	}()

	//头像2
	lazy var finchImageView = {
	    let snatch = UIImageView()

		snatch.layer.cornerRadius = 4;
    	snatch.layer.masksToBounds = true;
    	snatch.layer.borderWidth = 1;
    	snatch.layer.borderColor = UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1.0).cgColor;
        return snatch
	}()

	//头像3
	lazy var pythonImageView = {
	    let lace = UIImageView()

		lace.layer.cornerRadius = 4;
    	lace.layer.masksToBounds = true;
    	lace.layer.borderWidth = 1;
    	lace.layer.borderColor = UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1.0).cgColor;
        return lace
	}()

	//数字描述
	lazy var anteaterLabel = {
	    let falcon = UILabel()
    	falcon.textColor = UIColor(red: 102.0/255.0, green: 102.0/255.0, blue: 102.0/255.0, alpha: 1.0)
    	falcon.numberOfLines = 0
    	falcon.font = UIFont.systemFont(ofSize: 12);

		return falcon
	}()

	//申请按钮
	lazy var albatrossButton = {
	    let pitch = UIButton(type: .custom)
    	pitch.addTarget(self, action: #selector(mulberryOnSqingactionAction), for: .touchUpInside)
        pitch.setTitle("申请", for: .normal)
        pitch.setTitle("已满", for: .disabled)
        pitch.setTitleColor(.white, for: .normal)
        pitch.titleLabel?.font = .systemFont(ofSize: 12, weight: .regular)
        pitch.setBackgroundImage(UIImage(named:"join_bg"), for: .normal)
		pitch.layer.cornerRadius = 14;
    	pitch.layer.masksToBounds = true;
        return pitch
	}()
    
    @objc var model:TAMTHomeItem!  {
        didSet {
            llamaImageView.sd_setImage(with: NSURL(string: model.ellaImgAlice) as URL?, placeholderImage: placeImage)
            pigeonLabel.text = model.liamNameAlice
            salmonLabel.text = String(format: "团长：%@  ｜  成员：%ld/%ld", model.avaOwnerNameHarper, model.maxMembersBella, model.tomMaxMembersBella)
            anteaterLabel.text = String(format: "%ld人已加入", model.maxMembersBella)
            weaselLabel.attributedText = model.attString
            
            for i in 0..<avatarViews!.count {
                let imgView = avatarViews![i]
                imgView.isHidden = true
                if i<model.leoMemberAvatarsMason.count {
                    imgView.sd_setImage(with: NSURL(string: model.leoMemberAvatarsMason[i]) as URL?, placeholderImage: placeImage)
                    imgView.isHidden = false
                }
            }
            
            self.albatrossButton.isEnabled = model.maxMembersBella != model.tomMaxMembersBella
        }
    }
    
    let colors:[UIColor] = [UIColor(red: 249/255.0, green: 255/255.0, blue: 240/255.0, alpha: 1), UIColor(red: 240/255.0, green: 250/255.0, blue: 255/255.0, alpha: 1), UIColor(red: 255/255.0, green: 240/255.0, blue: 232/255.0, alpha: 1), UIColor(red: 255/255.0, green: 249/255.0, blue: 240/255.0, alpha: 1)]
    

    var index:Int? {
        didSet {
            flyView.backgroundColor = colors[index!%4]
        }
    }
    
    var avatarViews:[UIImageView]?
    

    override init(frame: CGRect) {
        super.init(frame: frame)

        formatInitLayoutUI()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        formatInitLayoutUI()
    }

    private func formatInitLayoutUI() {

        self.contentView.addSubview(flyView)
	    flyView.snp.makeConstraints { make in
        	make.top.equalTo(16);
        	make.bottom.equalTo(0);
        	make.leading.equalTo(16);
        	make.trailing.equalTo(-12);
    	}


        self.contentView.addSubview(llamaImageView)
	    llamaImageView.snp.makeConstraints { make in
        	make.height.equalTo(70);
        	make.width.equalTo(70);
        	make.top.equalToSuperview()
        	make.leading.equalTo(flyView).offset(12);
    	}


        self.contentView.addSubview(pigeonLabel)
	    pigeonLabel.snp.makeConstraints { make in
        	make.height.equalTo(20);
            make.top.equalTo(flyView).offset(12);
        	make.left.equalTo(llamaImageView.snp.right).offset(10);
    	}


        self.contentView.addSubview(salmonLabel)
	    salmonLabel.snp.makeConstraints { make in
        	make.leading.equalTo(pigeonLabel);
            make.bottom.equalTo(llamaImageView)
    	}


        self.contentView.addSubview(weaselLabel)
	    weaselLabel.snp.makeConstraints { make in
        	make.leading.equalTo(llamaImageView);
        	make.top.equalTo(llamaImageView.snp.bottom).offset(12);
            make.trailing.equalTo(flyView).offset(-16)
    	}


        self.contentView.addSubview(okapiImageView)
	    okapiImageView.snp.makeConstraints { make in
        	make.height.equalTo(24);
        	make.width.equalTo(24);
            make.bottom.equalTo(-16)
        	make.leading.equalTo(llamaImageView);
    	}
        
        self.contentView.addSubview(finchImageView)
        finchImageView.snp.makeConstraints { make in
            make.height.equalTo(24);
            make.width.equalTo(24);
            make.centerY.equalTo(okapiImageView);
            make.left.equalTo(okapiImageView.snp.right).offset(4);
        }
        
        self.contentView.addSubview(pythonImageView)
        pythonImageView.snp.makeConstraints { make in
            make.height.equalTo(24);
            make.width.equalTo(24);
            make.centerY.equalTo(okapiImageView);
            make.left.equalTo(finchImageView.snp.right).offset(4);
        }


        self.contentView.addSubview(albatrossButton)
	    albatrossButton.snp.makeConstraints { make in
        	make.height.equalTo(28);
        	make.width.equalTo(68);
            make.trailing.equalTo(flyView).offset(-16)
        	make.centerY.equalTo(okapiImageView);
    	}

        self.contentView.addSubview(anteaterLabel)
	    anteaterLabel.snp.makeConstraints { make in
        	make.height.equalTo(15);
        	make.centerY.equalTo(okapiImageView);
        	make.left.equalTo(pythonImageView.snp.right).offset(8);
    	}
        
        avatarViews = [okapiImageView, finchImageView, pythonImageView];
	}

	@objc func mulberryOnSqingactionAction() {
	}
}
